package com.sl.au.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.sl.au.entity.Part;
import com.sl.au.entity.PartBaseInfo;
import com.sl.au.entity.Goods;
import com.sl.au.entity.GridAreaInfo;
import com.sl.au.entity.Option;
import com.sl.au.service.PartService;
import com.sl.au.service.GoodsService;
import com.sl.au.service.IdentifyService;
import com.sl.au.service.OptionService;
import com.sl.au.service.UserService;
import com.sl.utils.ExdeUtil;
import com.sl.utils.MsgHelper;

/**
 * 部件管理
 * 
 * @author yangdi
 *
 */
@RestController
@RequestMapping(value = "/ui/goods")
public class GoodsController extends BaseController {

	@Autowired
	GoodsService goodsService;
	
	
	
	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView index(){
		ModelAndView modelAndView = this.createLayoutView("goods/goodsindex");
		return modelAndView;
	}
	
	
	/**
	 * 初始数据
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "getpage", method = RequestMethod.POST)
	public Map<String,Object> getPage(HttpServletRequest request) {
		Map<String,Object> map = new HashMap<String,Object>();
		int start = Integer.parseInt(request.getParameter("start"));
		int length = Integer.parseInt(request.getParameter("length"));
		String key=request.getParameter("key");
		List<Goods> modules=null;
		long count = 0;
		
		modules = goodsService.getPage(start, length, key, "createTime", Sort.Direction.DESC);
		count=goodsService.getPageCount(key);
		
		map.put("data", modules);
		map.put("draw", request.getParameter("draw"));
		map.put("recordsFiltered", count);
		map.put("recordsTotal", count);
		return map;
	}

	
	
	@RequestMapping(value = "saveGoodsWithFile", method = RequestMethod.POST)
	public MsgHelper saveGoodsWithFile(Goods goods,@RequestParam(value = "file", required = false) MultipartFile file) {
		MsgHelper msgHelper = null;
		if (file.getOriginalFilename() != null && !"".equals(file.getOriginalFilename())) {
			// 注意路径的构建
			String url = getUploadFilePath(this.fileUploadPath, "Goods", file);
			goods.setIconAddress(url);
		}
		
		if(!goods.getId().equals("")){
			Goods oldGoods = goodsService.getRepository().findOne(goods.getId());
			
			oldGoods.setGoodsName(goods.getGoodsName());
			oldGoods.setGoodsMemo(goods.getGoodsMemo());
			oldGoods.setGoodsPrice(goods.getGoodsPrice());
			
			if(goods.getIconAddress() != null ) {
				oldGoods.setIconAddress(goods.getIconAddress());
			}
			goodsService.getRepository().save(oldGoods);
			msgHelper = MsgHelper.success();
			msgHelper.setMsg("商品信息保存成功!");
		}else{
			Goods newGoodst = new Goods();
			
			newGoodst.setGoodsName(goods.getGoodsName());
			newGoodst.setGoodsMemo(goods.getGoodsMemo());
			newGoodst.setGoodsPrice(goods.getGoodsPrice());
			newGoodst.setIconAddress(goods.getIconAddress());
			
			goodsService.getRepository().save(newGoodst);
			
			msgHelper = MsgHelper.success();
			msgHelper.setMsg("商品信息新增成功!");
		}
		
		return msgHelper;
	}
	
	/**
	 * 删除信息
	 * @param id
	 * @return
	 */
	@RequestMapping("/delete/{id}")
	public MsgHelper deletePart(@PathVariable("id") String id) {
		MsgHelper msgHelper = MsgHelper.success();
		goodsService.getRepository().delete(id);
		return msgHelper;
	}
	
}
